var myConfig = require('./config.js')
var users = require('./user.js')

const envConfig = myConfig.getConfig();
const config = {
  debug: envConfig.debug,
  proxy: envConfig.api_host
}

const request=function(args){
  console.log(config.proxy)
  var app = getApp();
  args.url = config.proxy+args.url;
 
  var cookie = null;
 

  try {
    cookie = app.globalData.app_cookie;
  } catch (e) {
  }
  console.log("lock:::" + cookie);


  if (args.isLoginApi == null ){
    args.header = {
       'content-type': 'application/x-www-form-urlencoded',
        "Cookie": cookie
    }
    var thatSuccess = args.success;
    args.success=function(rs){
      if (isLogin(rs)){
        thatSuccess(rs);
      }else{
        users.login();
      }
     
    }
  }
  wx.request(args);
}

const isLogin=function(rs){
  var data=rs.data;
  if (typeof data == "String"){
    data=JSON.parse(data);
  }
  if (data.hasOwnProperty("login_status")){
    if (data.login_status == '300' || data.login_status == 300){
      return false;
    }
  }
  return true;
}

module.exports = {
  request: request,
  config:config
}